unit App.Imp.TAppSystemObjectImp;

interface

uses SmallStruct.Core,
  SmallStruct.Core.TSSSystemObject,
  SysUtils;

type
  TAppSystemObjectImp = class(TSSSystemObject)
  public
    procedure InitObject; override;
  public
    procedure InitObjectFactory;
    procedure InitSystemDatabaseConnection;
    procedure InitConfigurationLoader;
    procedure LoadClassTable;
    procedure InitClassTable(AClassTable: ISSClassTable); virtual;
    function GetSystemDatabaseConnection: string; virtual; abstract;
  end;

implementation

uses
  App.Imp.TAppClassTableLoaderImp,
  App.Imp.TAppConfigurationLoaderImp,
  SmallStruct.Action,
  SmallStruct.Action.TSSActionFactory,
  SmallStruct.ActionView,
  SmallStruct.ActionView.TSSActionViewFactory,
  SmallStruct.BusinessObject,
  SmallStruct.BusinessObject.TSSBusinessObjectFactory,
  SmallStruct.Controller,
  SmallStruct.Configuration.TSSBusinessObjectConfiguration,
  SmallStruct.Configuration.TSSDatabaseConnectionConfiguration,
  SmallStruct.Configuration,
  SmallStruct.Database,
  SmallStruct.Database.TSSADODatabaseDriver,
  SmallStruct.Database.TSSDatabaseConnectionFactory,
  SmallStruct.Model,
  SmallStruct.Model.TSSModelFactory,
  SmallStruct.View,
  SmallStruct.View.TSSViewFactory,
  SmallStruct.BusinessObject.TSSAdvancedBusinessObject,
  { Customize Object }
  SmallStruct.ActionView.TSSActionPanel,
  SmallStruct.Controller.TSSControllerNavigator;

const
  SS_SYSTEM_DATABASE_CONNECTION = 'SYSTEM_DATABASE_CONNECTION';

  { TAppSystemObjectImp }

procedure TAppSystemObjectImp.InitClassTable(AClassTable: ISSClassTable);
begin
  AClassTable.RegisterClass(SS_ACTION_PANEL_NAME, TSSActionPanel);
  AClassTable.RegisterClass(SS_CONTROLLER_NAVIGATOR_NAME, TSSControllerNavigator);
end;

procedure TAppSystemObjectImp.InitConfigurationLoader;
var
  ConfigurationLoaderConfiguration: TSSBusinessObjectConfiguration;
  ConfigurationLoader: ISSConfigurationLoader;
  BusinessObjectFactory: ISSBusinessObjectFactory;
begin
  (GetClassTable as ISSClassTable).RegisterClass(SS_CONFIGURATION_LOADER, TAppConfigurationLoaderImp);

  ConfigurationLoaderConfiguration := TSSBusinessObjectConfiguration.Create(nil);
  ConfigurationLoaderConfiguration.SetBusinessObjectName(SS_CONFIGURATION_LOADER);
  ConfigurationLoaderConfiguration.SetDatabaseConnectionName(SS_SYSTEM_DATABASE_CONNECTION);
  ConfigurationLoaderConfiguration.SetRequireDatabase(True);
  ConfigurationLoaderConfiguration.SetBaseScript(False);

  BusinessObjectFactory := (GetGlobalObjectTable as ISSObjectContainer).GetObject(SS_BUSINESS_OBJECT_FACTORY_NAME) as ISSBusinessObjectFactory;

  ConfigurationLoader := BusinessObjectFactory.GetBusinessObject(ConfigurationLoaderConfiguration) as ISSConfigurationLoader;

  (GetGlobalObjectTable as ISSObjectContainer).AppendObject(SS_CONFIGURATION_LOADER, ConfigurationLoader.GetObjectInstance);

  (GetClassTable as ISSClassTable).UnregisterClass(SS_CONFIGURATION_LOADER);
end;

procedure TAppSystemObjectImp.InitObject;
begin
  inherited;
  InitObjectFactory;
  InitSystemDatabaseConnection;
  InitConfigurationLoader;
  LoadClassTable;
  InitClassTable(GetClassTable as ISSClassTable);
end;

procedure TAppSystemObjectImp.InitObjectFactory;
begin
  (GetGlobalObjectTable as ISSObjectContainer).AppendObject(SS_DATABASE_CONNECTION_FACTORY_NAME, TSSDatabaseConnectionFactory.Create(Self));
  (GetGlobalObjectTable as ISSObjectContainer).AppendObject(SS_BUSINESS_OBJECT_FACTORY_NAME, TSSBusinessObjectFactory.Create(Self));
  (GetGlobalObjectTable as ISSObjectContainer).AppendObject(SS_MODEL_FACTORY_NAME, TSSModelFactory.Create(Self));
  (GetGlobalObjectTable as ISSObjectContainer).AppendObject(SS_ACTIONVIEW_FACTORY_NAME, TSSActionViewFactory.Create(Self));
  (GetGlobalObjectTable as ISSObjectContainer).AppendObject(SS_VIEW_FACTORY, TSSViewFactory.Create(Self));
  (GetGlobalObjectTable as ISSObjectContainer).AppendObject(SS_ACTION_FACTORY, TSSActionFactory.Create(Self));
end;

procedure TAppSystemObjectImp.InitSystemDatabaseConnection;
var
  DatabaseConnectionConfiguration: TSSDatabaseConnectionConfiguration;
  SystemDatabaseConnection: ISSDatabaseDriver;
begin
  (GetClassTable as ISSClassTable).RegisterClass(SS_ADO_DATABASE_DRIVER, TSSADODatabaseDriver);
  DatabaseConnectionConfiguration := TSSDatabaseConnectionConfiguration.Create(nil);
  DatabaseConnectionConfiguration.SetDriverClassName(SS_ADO_DATABASE_DRIVER);
  DatabaseConnectionConfiguration.SetConnectionName(SS_SYSTEM_DATABASE_CONNECTION);
  DatabaseConnectionConfiguration.SetConnectionString(GetSystemDatabaseConnection);

  DatabaseConnectionConfiguration.SetConnection;

  SystemDatabaseConnection := ((GetGlobalObjectTable as ISSObjectContainer).GetObject(SS_DATABASE_CONNECTION_FACTORY_NAME) as ISSDatabaseConnectionFactory).CreateDatabaseConnection(DatabaseConnectionConfiguration);

  (GetGlobalObjectTable as ISSObjectContainer).AppendObject(SS_SYSTEM_DATABASE_CONNECTION, SystemDatabaseConnection.GetObjectInstance);
end;

procedure TAppSystemObjectImp.LoadClassTable;
var
  ClassTableLoaderConfiguration: TSSBusinessObjectConfiguration;
  ClassTableLoader: ISSClassTableLoader;
begin
  (GetClassTable as ISSClassTable).RegisterClass(SS_CLASS_TABLE_LOADER, TAppClassTableLoaderImp);

  ClassTableLoaderConfiguration := TSSBusinessObjectConfiguration.Create(nil);
  ClassTableLoaderConfiguration.SetBusinessObjectName(SS_CLASS_TABLE_LOADER);
  ClassTableLoaderConfiguration.SetDatabaseConnectionName(SS_SYSTEM_DATABASE_CONNECTION);
  ClassTableLoaderConfiguration.SetRequireDatabase(True);
  ClassTableLoaderConfiguration.SetBaseScript(False);

  ClassTableLoader := ((GetGlobalObjectTable as ISSObjectContainer).GetObject(SS_BUSINESS_OBJECT_FACTORY_NAME) as ISSBusinessObjectFactory).GetBusinessObject(ClassTableLoaderConfiguration) as ISSClassTableLoader;

  ClassTableLoader.LoadClassTable(GetClassTable as ISSClassTable);

  ((GetGlobalObjectTable as ISSObjectContainer).GetObject(SS_BUSINESS_OBJECT_FACTORY_NAME) as ISSBusinessObjectFactory).FreeBusinessObject(ClassTableLoader as ISSBusinessObject);

  (GetClassTable as ISSClassTable).UnregisterClass(SS_CLASS_TABLE_LOADER);
end;

end.

